//
//  USChoosePayTypeView.m
//  us
//
//  Created by lhx on 2025/7/20.
//

#import "USChoosePayTypeView.h"
#import "TouchableMaskView.h"

@interface USChoosePayTypeView ()

 
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UIView *stickerMaskView;

@end

@implementation USChoosePayTypeView


- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self setupView];
 
    }
    return self;
}

 

- (void)styleAppear{
    if (self.hidden) {
        self.hidden = NO;
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(0);
        }];
        [UIView animateWithDuration:0.3 animations:^{
            [self.superview layoutIfNeeded];
        }completion:^(BOOL finished) {

        }];
        
        //允许点击背景消失
         
            if (!self.stickerMaskView) {
                //可以让当前view消失，而且不影响后面的点击事件
                TouchableMaskView *maskView = [[TouchableMaskView alloc] initWithFrame:self.superview.bounds];
                maskView.backgroundColor = UIColor.clearColor;
                maskView.userInteractionEnabled = YES;
                @weakify(self)
                maskView.onTapOutside = ^{
                    @strongify(self)
                    [self styleAppear];
                };
                _stickerMaskView = maskView;
            }
            [self.superview insertSubview:self.stickerMaskView belowSubview:self];
         
    }else {
        if (self.stickerMaskView) {
            [self.stickerMaskView removeFromSuperview];
            self.stickerMaskView = nil;
        }
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_offset(self.jk_height);
        }];
        
        [UIView animateWithDuration:0.3 animations:^{
            [self.superview layoutIfNeeded];
        }completion:^(BOOL finished) {
            self.hidden = YES;
        }];
    }
    
 
}
#pragma mark - Setup

- (void)setupView {
    self.backgroundColor = UIColor.mi_1_color;
    self.layer.cornerRadius = 14;
    self.layer.masksToBounds = YES;
    
    UIView *toolView = [[UIView alloc]init];
    [self addSubview:toolView];
    [toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.mas_equalTo(44);
    }];
    
    self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.closeButton setImage:[UIImage imageNamed:@"normal_close"] forState:0];
    [self.closeButton addTarget:self action:@selector(closeButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [toolView addSubview:self.closeButton];
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(toolView).offset(16);
        make.centerY.equalTo(toolView);
        make.width.height.mas_equalTo(28);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
    titleLabel.text = @"选择支付方式";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [toolView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(toolView);
    }];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = UIColor.mi_3_color.CGColor;
    shapeLayer.lineWidth = 1.2;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.lineDashPattern = @[@8, @8];

    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(16, 43)];
    [path addLineToPoint:CGPointMake(ScreenWidth - 32, 44)];
    
    shapeLayer.path = path.CGPath;
    [self.layer addSublayer:shapeLayer];

    
    UIButton *aliPay = [UIButton us_fillButtonWithTitle:@"支付宝支付"];
    aliPay.tag = 1001;
    [aliPay addTarget:self action:@selector(selectPayAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:aliPay];
    [aliPay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_safeAreaLayoutGuideBottom);
        make.left.equalTo(self.mas_left).offset(32);
        make.right.equalTo(self.mas_right).offset(-32);
        make.height.mas_equalTo(56);
    }];
    
    
    UIButton *wxPay = [UIButton us_fillButtonWithTitle:@"微信支付"];
    [self addSubview:wxPay];
    wxPay.tag = 1000;
    [wxPay addTarget:self action:@selector(selectPayAction:) forControlEvents:UIControlEventTouchUpInside];
    [wxPay mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(aliPay.mas_top).offset(-20);
        make.left.equalTo(self.mas_left).offset(32);
        make.right.equalTo(self.mas_right).offset(-32);
        make.height.mas_equalTo(56);
    }];
    
}

- (void)selectPayAction:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(payTypedidSelectType:)]) {
        [self.delegate payTypedidSelectType:sender.tag - 1000];
        
        [self styleAppear];
    }
}

-(void)closeButtonTapped{
    [self styleAppear];
}


@end
